package cn.edu.jlu.community.app.post;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVFile;
import com.squareup.picasso.Picasso;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cn.edu.jlu.community.R;
import cn.edu.jlu.community.server.AppServer;
import cn.edu.jlu.community.util.Log;


public class PostListAdapter extends BaseAdapter {

    private Context mContext;
    private List<Map<String, Object>> mDataSet;
    private Map<Integer, ImageView> mImageLoadState = new HashMap<>();

    public PostListAdapter(Context context, List<Map<String, Object>> dataSet) {
        mContext = context;
        mDataSet = dataSet;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = LayoutInflater.from(mContext)
                    .inflate(R.layout.post_list_item, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }

        Map<String, Object> data = mDataSet.get(position);
        holder.title.setText(data.get("title").toString());
        holder.author.setText(data.get("author").toString());
        holder.commentNumber.setText(data.get("comment_count").toString());
        Date date = (Date) data.get("time");
        DateFormat formatter = SimpleDateFormat.getDateTimeInstance();
        holder.time.setText(formatter.format(date));

//        holder.portrait.setImageDrawable(parent.getResources().getDrawable(R.drawable.ic_head));
        AppServer.loadPortraitUrl(data.get("author_id").toString(),
                new PortraitCallback(holder.portrait, position));

        return convertView;
    }

    private class PortraitCallback implements AppServer.LoaderCallback {
        private ImageView mPortrait;
        private int mPosition;

        PortraitCallback(ImageView imageView, int position) {
            mPortrait = imageView;
            mPosition = position;
        }

        @Override
        public void onFinish(final Object data) {
            Picasso.with(mContext).load(((AVFile) data).getUrl()).into(mPortrait);
            mImageLoadState.put(mPosition, mPortrait);
        }

        @Override
        public void onError(AVException e) {

        }
    }

    static class ViewHolder {
        @InjectView(R.id.portrait)
        ImageView portrait;
        @InjectView(R.id.post_title)
        TextView title;
        @InjectView(R.id.post_time)
        TextView time;
        @InjectView(R.id.post_author)
        TextView author;
        @InjectView(R.id.post_comment_number)
        TextView commentNumber;

        public ViewHolder(View view) {
            ButterKnife.inject(this, view);
        }
    }

    @Override
    public int getCount() {
        return mDataSet.size();
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public Object getItem(int position) {
        Map<String, Object> data = mDataSet.get(position);
        ImageView portrait = mImageLoadState.get(position);
        if (null != portrait) {
            data.put("portrait", portrait.getDrawable());
        }
        return data;
    }
}
